package com.runa.monitor.platinterface.backwork.impl;

import com.alibaba.fastjson.JSONObject;
import com.runa.monitor.comm.dto.newComm.Archive;
import com.runa.monitor.comm.dto.newComm.AsynResult;
import com.runa.monitor.platinterface.backwork.function.TaskFunction;
import com.runa.monitor.platinterface.mqtt.MqttUtil;
import com.runa.monitor.platinterface.netservice.connect.commstatus.CommStatus;
import com.runa.rccprotocol.dto.AffirmDeny;
import lombok.extern.slf4j.Slf4j;

import java.util.List;

@Slf4j
public class ReadArchiveTaskFunction implements TaskFunction<List<Integer>, Object, Void> {

    private final String task;

    private final String holdInfo;

    private final int priority;

    private final String netEquId;

    private List<Integer> indexes;

    public ReadArchiveTaskFunction(String task, String holdInfo, int priority, String netEquId) {
        this.task = task;
        this.holdInfo = holdInfo;
        this.priority = priority;
        this.netEquId = netEquId;
    }

    @Override
    public void setStatus(List<Integer> indexes) {
        this.indexes = indexes;
    }

    @Override
    public void doApply(CommStatus status, Object result) {
        log.info("ReadArchiveTaskFunction daApply {} {}", status, JSONObject.toJSONString(result));
        AsynResult asynResult = AsynResult.success();
        asynResult.setTask(task);
        asynResult.setHoldInfo(holdInfo);
        asynResult.setIdentity(netEquId);
        if (CommStatus.OK.equals(status)) {
            if (result instanceof List) {
                List<List<Archive>> list = (List<List<Archive>>) result;
                list.forEach(archives -> {
                    for (Archive archive : archives) {
                        asynResult.setContent(archive);
                        indexes.remove(archive.getIndex());
                        MqttUtil.send(Archive.TOPIC, asynResult);
                    }
                });
                asynResult.noSuchFileSetting();
                for (Integer index : indexes) {
                    Archive archive = new Archive();
                    archive.setIndex(index);
                    asynResult.setContent(archive);
                    MqttUtil.send(Archive.TOPIC, asynResult);
                }
            } else if (result instanceof AffirmDeny) {
                if (!((AffirmDeny) result).getData()) {
                    for (Integer index : indexes) {
                        Archive archive = new Archive();
                        archive.setIndex(index);
                        asynResult.setContent(archive);
                        asynResult.noSuchFileSetting();
                        MqttUtil.send(Archive.TOPIC, asynResult);
                    }
                }
            }
        } else if (CommStatus.OffLine.equals(status)) {
            List<Integer> indexes = (List<Integer>) result;
            for (Integer index : indexes) {
                asynResult.offlineSetting();
                Archive archive = new Archive();
                archive.setIndex(index);
                asynResult.setContent(archive);
                MqttUtil.send(Archive.TOPIC, asynResult);
            }
        } else if (CommStatus.TimeOut.equals(status)) {
            for (Integer index : indexes) {
                asynResult.timeoutSetting();
                Archive archive = new Archive();
                archive.setIndex(index);
                asynResult.setContent(archive);
                MqttUtil.send(Archive.TOPIC, asynResult);
            }
        }
    }

    @Override
    public Void waitResult() {
        return null;
    }

    @Override
    public String getSerial() {
        return null;
    }

    @Override
    public int getPriority() {
        return priority;
    }
}
